/******************************************************************************

 ******************************************************************************
  File Name	:video.h
  Version		:001
  Author		:liangH
  Description	:define video class
  History		:20120612 new
  Modification	: 

******************************************************************************/
#ifndef __VIDEO_H__
#define __VIDEO_H__

#include "config.h"

typedef struct hiGET_STREAM_S
{
    HI_BOOL        bThreadStart;
    pthread_t       pid;
    PAYLOAD_TYPE_E enPayload;/*ven channel is H.264? MJPEG? or JPEG?*/
    VENC_CHN VeChnStart;/*From this channel to get stream.*/
    HI_S32 s32ChnTotal;/*how many channels to get stream from. 
                                       * channel index is VeChnStart, VeChnStart+1, ..., VeChnStart+(s32ChnTotal-1).
                                       * Used for SampleGetVencStreamProc.*/
    HI_S32 s32SaveTotal;/*how many frames will be get each channel.*/
                                /*Note: JPEG snap, it must be 1. */
    HI_U8 aszFileNoPostfix[32]; /*complete file name will add postfix automaticly: (not finish yet)
                                         * _chnX.h264.  -- for h.264
                                         * _chnX.mjp.   -- for mjpeg
                                         * _chnX_Y.jpg  -- for jpeg
                                         * */
} GET_STREAM_S;

typedef struct tag_SNAP_INDEX_ITEM
{
	unsigned long m_signature;/*the flag of this struct, INDEX_ITEM_SIGNATURE*/
	unsigned short m_file_size;/*file size*/
	
	unsigned char	m_state;/*the state that if file is deleted. 0x5a shows file is deleted, 0x01 shows curent file and other shows file exits. the initialization value is 0*/
	unsigned char	m_year;
	unsigned char m_month;
	unsigned char m_mday;

	int m_stime_hms;/*the start time of file,  =h*3600+m*60+s*/
	
	unsigned char m_record_type;/*recorder type, 1 shows normal snapping, 2 shows handle recorder*/

	char	m_dir[32];/*file directory*/
	char	m_name[TOTAL_SNAP_NAME_LEN + 1];/*file name that dose not contain path*/
}SNAP_INDEX_ITEM;

/* vou device enumeration */
typedef enum hiVO_DEV_E
{
    VO_DEV_HD  = 0,                 /* high definition device */
    VO_DEV_AD  = 1,                 /* assistant device */
    VO_DEV_SD  = 2,                 /* spot device */
    VO_DEV_BUTT
} VO_DEV_E;

class Video{
	/*variable*/
	public: 
		/*static GET_STREAM_S video_enc_stream;*/
	private: 
		unsigned char video_flag[4];/*video flag of four channel that shows if server requests real-time video data*/
		unsigned char video_quality[4];/*video quality of four channel*/
		unsigned char video_resolution[4];/*video resolution of four channel. 0 shows QCIF, 1 shows CIF, 3 shows HD1 and 4 shows D1.*/
		unsigned int video_code_stream[4];/*video code stream of four channel*/
		unsigned char video_frame_rate[4];/*video frame rate of four channel*/
		unsigned short video_I_frame_interval[4];/*the interval of I frame of four channel*/
		unsigned char video_code_stream_flag[4];/*video code stream flag of four channel, static or dynamic code stream*/
		unsigned char video_brightness[4];/*video brightness of four channel*/
		unsigned char video_chromaticity[4];/*video chromaticity of four channel*/
		unsigned char video_saturation[4];/*video saturation of four channel*/
		unsigned char video_gray_scal[4];/*video gray scal of four channel*/
		unsigned char video_contrast[4];/*video contrast of four channel*/
		pthread_t p_transmission_id;/*thread of transmission*/
		
		int network_send_socket[4];/*the socket of sending network data*/
		unsigned int recorder_port[4];/*recorder port, default is 18886, 18887, 18888, 18889*/
		unsigned int network_port[4];/*recorder port, default is 30002 that is saved in config file*/

		HI_BOOL video_output;/*the flag of video output*/
		unsigned char snap_type;/*the type of snapping*/
		HI_BOOL manual_snap_flag;/*the flag of manual snapping*/
		unsigned char manual_snap_channel;/*the channel of manual snapping*/
		HI_BOOL i_frame_flag[4];/*the flag of I frame request that is used for new recorder packet*/
		char search_snap_file_name[47];/*the snap file name of searching result, that contains clientID of PC, mac address and time*/
		unsigned char search_snap_flag;/*the flag of searching snap file*/
		unsigned char search_snap_channelID;/*the channelID of snap file to be searched*/
		unsigned char search_snap_start_time[14];/*the start time of snap file to be searched. the format is YYYYMMDDHHMMSS: 20121201112035*/
		unsigned char search_snap_end_time[14];/*the end time of snap file to be searched. the format is YYYYMMDDHHMMSS: 20121202112500*/
		unsigned char search_snap_type;/*the type of snap file to be searched*/
		unsigned char search_snap_source;/*the source of snap file to be searched*/
		unsigned char snap_addr[4];/*the address of snap, 1 shows sd and 2 shows hdd*/
		unsigned char video_loss_channel;/*the channel number of video loss*/
		unsigned char video_norm[4];/*video norm: PAL or NTSC norm*/
		pthread_mutex_t read_write_lock;/*read and write lock*/
		pthread_mutex_t snap_list_lock;/*snapping list lock that is used for snapping and searching snap*/
		char send_snap_file[128];/*the file name of sending snap file in snap_thread_send pthread*/
		char send_snap_list[128];/*the file name of sending snap list in snap_list_thread_send pthread*/
		unsigned char snap_list_sts;/*the status of sending snap list thread*/
		unsigned char snap_file_sts;/*the status of sending snap file thread*/
		/*constructed function and deconstructed function*/
	public:
		Video();/*constructed function*/
		~Video();/*deconstructed function*/

	public:
	/*set,get member functin*/
		void set_video_flag(unsigned char channelID, unsigned char temp_snap_flag);
		unsigned char get_video_flag(unsigned char channelID);
		void set_video_quality(unsigned char channelID, unsigned char temp_video_quality);
		unsigned char get_video_quality(unsigned char channelID);
		void set_video_resolution(unsigned char channelID, unsigned char temp_video_resolution);
		unsigned char get_video_resolution(unsigned char channelID);
		void set_video_code_stream(unsigned char channelID, unsigned char temp_video_code_stream);
		unsigned int get_video_code_stream(unsigned char channelID);
		unsigned char get_video_code_stream_pc(unsigned char channelID);
		void set_video_frame_rate(unsigned char channelID, unsigned char temp_video_frame_rate);
		unsigned char get_video_frame_rate(unsigned char channelID);
		unsigned char get_video_frame_rate_pc(unsigned char channelID);
		void set_video_I_frame_interval(unsigned char channelID, unsigned char temp_video_I_frame_interval);
		unsigned short get_video_I_frame_interval(unsigned char channelID);
		unsigned char get_video_I_frame_interval_pc(unsigned char channelID);
		void set_video_code_stream_flag(unsigned char channelID, unsigned char temp_video_code_stream_flag);
		unsigned char get_video_code_stream_flag(unsigned char channelID);
		void set_video_brightness(unsigned char channelID, unsigned char temp_video_brightness);
		unsigned char get_video_brightness(unsigned char channelID);		
		void set_video_chromaticity(unsigned char channelID, unsigned char temp_video_chromaticity);
		unsigned char get_video_chromaticity(unsigned char channelID);
		void set_video_saturation(unsigned char channelID, unsigned char temp_video_saturation);
		unsigned char get_video_saturation(unsigned char channelID);
		void set_video_gray_scal(unsigned char channelID, unsigned char temp_video_gray_scal);
		unsigned char get_video_gray_scal(unsigned char channelID);
		void set_video_contrast(unsigned char channelID, unsigned char temp_video_contrast);
		unsigned char get_video_contrast(unsigned char channelID);
		void set_snap_addr(unsigned char channelID, unsigned char temp_snap_addr);
		unsigned char get_snap_addr(unsigned char channelID);
		void set_video_norm(unsigned char channelID, unsigned char temp_norm);
		unsigned char get_video_norm(unsigned char channelID);
		HI_BOOL get_video_output();
		void set_video_output(HI_BOOL temp_video_output);
		HI_BOOL get_manual_snap_flag();
		void set_manual_snap_flag(HI_BOOL temp_manual_snap_flag);
		HI_BOOL get_i_frame_flag(unsigned char channelID);
		void set_i_frame_flag(unsigned char channelID, HI_BOOL temp_i_frame_flag_1);
		void set_snap_type(unsigned char temp_snap_type);
		unsigned char get_snap_type();
		void set_manual_snap_channel(unsigned char temp_manual_snap_channel);
		unsigned char get_manual_snap_channel();
		void set_recorder_port(unsigned char channelID, unsigned int temp_recorder_port);
		unsigned int get_recorder_port(unsigned char channelID);
		void set_network_port(unsigned char channelID, unsigned int temp_network_port);
		unsigned int get_network_port(unsigned char channelID);
		//int get_recorder_send_socket(unsigned char channelID);
		int get_network_send_socket(unsigned char channelID);
		int close_network_send_socket();
		void set_search_snap_file_name(char * temp_search_file_name);
		void get_search_snap_file_name(char * temp_search_file_name);
		void set_search_snap_flag(unsigned char temp_search_snap_flag);
		unsigned char get_search_snap_flag();
		void set_search_snap_channelID(unsigned char temp_snap_channleID);
		unsigned char get_search_snap_channelID();
		void set_search_snap_start_time(unsigned char * temp_snap_start_time);
		void get_search_snap_start_time(unsigned char * temp_snap_start_time);
		void set_search_snap_end_time(unsigned char * temp_snap_end_time);
		void get_search_snap_end_time(unsigned char * temp_snap_end_time);
		void set_search_snap_type(unsigned char temp_snap_type);
		unsigned char get_search_snap_type();
		void set_search_snap_source(unsigned char temp_snap_source);
		unsigned char get_search_snap_source();
		void set_video_loss_channel(unsigned char temp_video_loss_channel);
		void clear_video_loss_channel(unsigned char temp_video_loss_channel);
		unsigned char get_video_loss_channel();
		void set_send_snap_file(char * temp_send_snap_file);
		void get_send_snap_file(char * temp_send_snap_file);
		void clear_send_snap_file();
		void set_send_snap_list(char * temp_send_snap_list);
		void get_send_snap_list(char * temp_send_snap_list);
		void clear_send_snap_list();
		void set_snap_list_sts(unsigned char temp_snap_list_sts);
		unsigned char get_snap_list_sts();
		void set_snap_file_sts(unsigned char temp_snap_file_sts);
		unsigned char get_snap_file_sts();
	/*set,get member functin end*/

	/*basic function*/
		HI_S32 save_venc_stream(PAYLOAD_TYPE_E payload_type, FILE *p_fd, VENC_STREAM_S *venc_stream);
		HI_S32 save_mjpeg_stream(FILE* fp_mjpeg_file, VENC_STREAM_S *mjpeg_stream);
		HI_S32 get_snap_pic(VENC_CHN snap_chn, FILE *p_file);
		HI_S32 venc_4d1_4cif_h264(unsigned char channelID);/*video encode of 4d1 and 4 cif*/
		HI_S32 initialize_video_encoder(unsigned char channelID, HI_U32 group_cnt, HI_BOOL have_minor, PAYLOAD_TYPE_E payload_type[2], PIC_SIZE_E pic_size[2]);
		HI_S32 initialize_video_encoder_single(unsigned char channelID, HI_BOOL have_minor, PAYLOAD_TYPE_E payload_type[2], PIC_SIZE_E pic_size[2]);
		static HI_S32 stop_venc_get_stream();
		HI_S32 stop_venc_flag();
		static HI_S32 start_venc_get_stream(GET_STREAM_S *get_venc_stream);
		HI_S32 stop_video_encode(unsigned char channelID, HI_U32 group_count, HI_BOOL have_minor);
		HI_S32 stop_video_encode_single(unsigned char channelID, HI_BOOL have_minor);
		HI_S32 exit_mpp();

		HI_S32 tw2866_setting(VIDEO_CH_E enCh,VIDEO_TYPE_E enVideoMode,HI_S32 val);/*add by dujian*/
		HI_S32 stop_transmission_thread();
		HI_S32 stop_osd(unsigned char channelID, unsigned char areaID);
		HI_S32 create_jpeg_chn(HI_U32 group_cnt, PIC_SIZE_E pic_size);/*create four snapping channels*/
		HI_S32 start_snap_by_channelID(unsigned char channelID, unsigned char snap_type);/*snapping by channel*/
		int search_snap_index_file(char *dir_path);/*search snap index file and create result file*/
		int search_snap_index_file_again(char *dir_path);/*search snap index file and create result file*/
		int get_snap_file(char *dir_path, char * file_name);/*search index file by filename, then send this file to FTP*/
		int stop_business_thread(unsigned char thread_flag);/*stop all pthread before shutdown or reboot*/
		unsigned long long mscount(unsigned long long count);/*get the time difference with count*/
		HI_S32 initialize_socket();
	/*basic function end*/
	
	private:	
	/*inside process function*/
		HI_S32 initialize_mpp(VB_CONF_S *pst_video_buffer);
		/*HI_S32 initialize_vivo_sd(unsigned char channelID, HI_S32 channel_total, PIC_SIZE_E video_size, VO_DEV video_output_dev);*//*20130117 lianghao delete the parameter of video_size*/
		HI_S32 initialize_vivo_sd(unsigned char channelID, HI_S32 channel_total, VO_DEV video_output_dev);
		/*HI_S32 initialize_vi_sd(unsigned char channelID, HI_S32 vi_channel_total, PIC_SIZE_E video_size);*//*20130117 lianghao delete the parameter of video_size*/
		HI_S32 initialize_vi_sd(unsigned char channelID, HI_S32 vi_channel_total);
		HI_S32 initialize_vo_sd(unsigned char channelID, HI_S32 vo_channel_total, VO_DEV video_output_dev);
		HI_S32 tw2866_config(VIDEO_NORM_E video_norm,VI_WORK_MODE_E vi_work_mode);
		HI_S32 start_video_input(unsigned char channelID, HI_S32 channel_total, VI_PUB_ATTR_S* vi_dev_attr, VI_CHN_ATTR_S* vi_chn_attr);
		HI_S32 get_vi_chn_per_dev(VI_INPUT_MODE_E vi_input_mode, VI_WORK_MODE_E vi_work_mode);
		HI_S32 start_video_output(unsigned char channelID, HI_S32 channel_total, VO_DEV video_output_dev, VO_PUB_ATTR_S* vo_dev_attr, VO_VIDEO_LAYER_ATTR_S* video_layer_attr);
		HI_S32 start_vo_device(VO_DEV vo_dev, VO_PUB_ATTR_S* dev_attr);
		HI_S32 start_vo_video_layer(VO_DEV vo_dev, VO_VIDEO_LAYER_ATTR_S* video_layer_attr);
		HI_S32 set_vo_chn_attr(VO_DEV vo_dev, HI_U32 channel_counter, HI_U32 width, HI_U32 height);
		HI_S32 set_h264_config(unsigned char channelID, PIC_SIZE_E pic_size, HI_BOOL main_stream, VENC_ATTR_H264_S *h264_attr);
		HI_S32 set_mjpeg_config(unsigned char channelID, PIC_SIZE_E pic_size, HI_BOOL main_stream, VENC_ATTR_MJPEG_S *mjpeg_attr);
		HI_S32 get_group_vi_map(VENC_GRP ve_group, VI_DEV *vi_dev, VI_CHN *vi_chn);
		HI_S32 save_h264_stream(FILE* fp_h264_file, VENC_STREAM_S *h264_stream);
		HI_S32 create_jpeg_chn_single(unsigned char channelID, VENC_GRP ve_group, VENC_CHN snap_chn, PIC_SIZE_E pic_size);
		HI_S32 set_jpeg_config(unsigned char channelID, PIC_SIZE_E pic_size, VENC_ATTR_JPEG_S *jpeg_attr);
		int write_i2c(unsigned char ch, unsigned char base, int reg_value);/*add by dujian*/
		HI_VOID get_vi_confg_sd(unsigned char channelID, PIC_SIZE_E pic_size, VI_PUB_ATTR_S* vi_dev_attr, VI_CHN_ATTR_S* vi_chn_attr);

		/* wait for decoder finished and destroy channel.Stream left in decoder will be decoded. */
		HI_VOID get_vo_cfg_1280x1024_4screen(VO_PUB_ATTR_S* vo_pub_attr, VO_VIDEO_LAYER_ATTR_S* vo_layer_attr);
		HI_S32 vi_bind_vo(HI_S32 vi_chn_total, VO_DEV vo_dev);
		int create_snap_index_file(char *dir_path, unsigned char snap_type);/*create index file of snapping*/
		int delete_snap_index_file(char *dir_path);/*create index file of snapping*/
		int get_snap_start_time(char *file_name);/*get start time from recorder name*/
		int init_config_file();
	/*inside process function end*/
	
};

#endif

